package com.jasperframework.boot.common.utils;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.util.List;


/**
  * <p> Thumbnails缩略图处理工具 </p>
  *
  * @site https://miliqkdoc.motopa.cn/
  * @author daizhao
  * @date 2022/12/12 12:12
  */
public class ThumbnailsUtils {

	private static final Logger logger = LoggerFactory.getLogger(ThumbnailsUtils.class);

	/**
	 *
	 * @param srcImgPath	原图全路径
	 * @param outImgPath	输出全路径
	 * @param RUNNING_SHELL_DIR	     脚本目录
	 * @param RUNNING_SHELL_FILE  脚本文件
	 * @param opencv_width  压缩宽度
	 * @param opencv_height 压缩高度
	 * @return
	 * @throws IOException
	 */
	public static List<String> opencvOfFaceSwapper(String srcImgPath, String outImgPath, String RUNNING_SHELL_DIR, String RUNNING_SHELL_FILE, int opencv_width, int opencv_height, String templateName) throws IOException{
		logger.info("srcImgPath=["+srcImgPath+"]");
		logger.info("outImgPath=["+outImgPath+"]");
		//压缩图片
		ThumbnailsUtils.zoomImage(srcImgPath, outImgPath, true, opencv_width, opencv_height);
		return ProcessBuilderUtils.runLinuxCommand(RUNNING_SHELL_DIR, RUNNING_SHELL_FILE, outImgPath, templateName);
	}

	/**
	 * 按照指定比例的宽度和高度缩放图片,压缩成缩略图, 遵循原图比例缩
	 * @param srcImagePath  原图路径
	 * @param outImagePath  输出图路径
	 * @param keepAspectRatio 是否遵循原图比例进行缩放
	 * @param width			宽
	 * @param height		高
	 */
	public static void zoomImage(String srcImagePath, String outImagePath, boolean keepAspectRatio , int width, int height){
		//此处把图片压成width×height的缩略图
		try {
			Thumbnails.of(srcImagePath).size(width,height).keepAspectRatio(keepAspectRatio).toFile(outImagePath);
		} catch (IOException e) {

		}
	}

	/**
	 * 按比例缩放图片
	 * @param srcImagePath	原图全路径
	 * @param outImagePath	输出图路径
	 * @param scale			缩放比例，大于1就是变大，小于1就是缩小
	 */
	public static void scaleImage(String srcImagePath, String outImagePath, float scale){
		try {
			Thumbnails.of(srcImagePath).scale(scale).toFile(outImagePath);//按比例缩小   scale = 0.2f/2f
		} catch (IOException e) {

		}
	}

	/**
	 * 旋转图片
	 * @param srcImagePath	原图全路径
	 * @param outImagePath	输出图路径
	 * @param rotate		rotate(角度),正数则为顺时针，负数则为逆时针
	 */
	public static void rotateImage(String srcImagePath, String outImagePath, int rotate){
		try {
			Thumbnails.of(srcImagePath).scale(1f).rotate(rotate).toFile(outImagePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 图片尺寸不变，压缩图片文件大小outputQuality实现
	 * @param srcImagePath	原图全路径
	 * @param outImagePath	输出图路径
	 * @param quanlity		参数1为最高质量  ,小于1低质量
	 */
	public static void compressImage(String srcImagePath, String outImagePath, float quanlity){
		try {
			Thumbnails.of(srcImagePath).scale(1f).outputQuality(quanlity).toFile(outImagePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 给指定位置图片加水印，watermark(位置，水印图，透明度)Positions.CENTER表示加在中间
	 * @param srcImagePath	   原图全路径
	 * @param outImagePath	   输出图路径
	 * @param waterImagePath 水印图路径
	 * @param width			宽
	 * @param height		高
	 */
	public static void waterMark(String srcImagePath, String outImagePath, String waterImagePath, int width, int height){
		try {
			Thumbnails.of(srcImagePath).size(width, height)
			.watermark(Positions.CENTER, ImageIO.read(new File(waterImagePath)), 0.5f)
			.outputQuality(0.8f).toFile(outImagePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 图片裁剪     用sourceRegion()实现   Positions.CENTER表示中心，还有许多其他位置可选
	 * @param srcImagePath	原图全路径
	 * @param outImagePath	输出图路径
	 * @param width			宽
	 * @param height		高
	 */
	public static void cutImage(String srcImagePath, String outImagePath, int width, int height){
		try {
			Thumbnails.of(srcImagePath).sourceRegion(Positions.CENTER,width,height).size(width,height).toFile(outImagePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 图片格式转换，保存原尺寸不变
	 * @param srcImagePath	原图全路径
	 * @param outImagePath	输出图路径
	 * @param suffix		格式
	 */
	public static void transferFormate(String srcImagePath, String outImagePath, String suffix){
		try {
			Thumbnails.of(srcImagePath).scale(1f).outputFormat(suffix).toFile(outImagePath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
